package com.application.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.application.R;
import com.application.bean.NewsBean;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private List<NewsBean> newsBeanList;
    private LayoutInflater layoutInflater;
    private Context context;

    public MyAdapter(Context context,List<NewsBean> newsBeanList) {
        this.newsBeanList = newsBeanList;
        this.context = context;
        layoutInflater=LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=layoutInflater.inflate(R.layout.list_item_layout,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        NewsBean newsBean = newsBeanList.get(position);
        holder.tvTitle.setText(newsBean.getTitle());
        holder.tvContent.setText(newsBean.getContent());
        holder.ivImage.setImageResource(newsBean.getImageResourceId());
        holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"你点击了"+position,Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return newsBeanList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView tvTitle;
        TextView tvContent;
        ImageView ivImage;
        RelativeLayout relativeLayout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            this.ivImage=itemView.findViewById(R.id.iv_recycler_img);
            this.tvTitle=itemView.findViewById(R.id.tv_recycler_title);
            this.tvContent=itemView.findViewById(R.id.tv_recycler_content);
            this.relativeLayout=itemView.findViewById(R.id.rl_item_container);

        }
    }
}
